package com.guli.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.MD5;
import com.guli.educenter.entity.UcenterMember;
import com.guli.educenter.entity.vo.RegisterVo;
import com.guli.educenter.mapper.UcenterMemberMapper;
import com.guli.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author LMZovo
 * @since 2022-02-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败");
        }
        QueryWrapper<UcenterMember> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper1);
        if (mobileMember == null) {
            throw new GuliException(20001, "手机号信息不存在");
        }
        //判断密码
        //MD5加密
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001, "密码错误");
        }
        //判断是否被禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败");
        }
        //登录成功
        //生成token字符串
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile();//手机号
        String password = registerVo.getPassword(); //密码
        String nickname = registerVo.getNickname(); //昵称

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001, "信息不能为空，注册失败");
        }

        //判断输入的验证码和redis中存放的验证码是否相同
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误");
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        int count = count(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "手机号重复");
        }

        //向数据库中添加数据
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false); //不禁用
        member.setAvatar("beijing.aliyuncs.com/2020/08/14/3d66cd734fa44cf988dcb88a2c11785bfile.png"); //默认头像
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember;
    }

    //查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {
        //return baseMapper.countRegisterDay(day);
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.like("gmt_create", day);
        return baseMapper.selectCount(wrapper);
    }


}